package com.neiru.divider.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.neiru.divider.bean.Level;
import com.neiru.divider.dao.constants.LevelConstants;
import com.neiru.divider.dao.helper.DatabaseHelper;

public class LevelDao {
	private Context context;
	private DatabaseHelper dbHelper;
	private SQLiteDatabase db;
	public LevelDao(Context context) {
		this.context = context;
	}
	public LevelDao open() throws SQLException {
		dbHelper = new DatabaseHelper(context);
		db = dbHelper.getWritableDatabase();
		return this;
	}
	
	public LevelDao close() {
		db.close();
		return this;
	}
	public void updateLevel(Level level) {
		deleteLevel(level);
		createLevel(level);
	}
	
	public void deleteLevel(Level level) {
		String[] stringVacio = {};
		db.delete(LevelConstants.DATABASE_LEVELS_TABLE,LevelConstants.KEY_LEVEL+" = '"+level.getLevel()+"'",stringVacio);
	}
	
	public void createLevel(Level level) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(LevelConstants.KEY_LEVEL, level.getLevel());
		initialValues.put(LevelConstants.KEY_BALLS, level.getBalls());
		initialValues.put(LevelConstants.KEY_BALLS_RADIO, level.getBallsRadio());
		initialValues.put(LevelConstants.KEY_SPEED, level.getSpeed());
		initialValues.put(LevelConstants.KEY_UNLOCKED, level.getUnlocked());
		initialValues.put(LevelConstants.KEY_SPEED, level.getSpeed());
		
		initialValues.put(LevelConstants.KEY_BEST_SCORE, 
				(level.getBestScores().size() > 0)?level.getBestScores().get(0):0);
		initialValues.put(LevelConstants.KEY_SECOND_BEST_SCORE, 
				(level.getBestScores().size() > 1)?level.getBestScores().get(1):0);
		initialValues.put(LevelConstants.KEY_THIRD_BEST_SCORE, 
				(level.getBestScores().size() > 2)?level.getBestScores().get(2):0);
		initialValues.put(LevelConstants.KEY_FOURTH_BEST_SCORE, 
				(level.getBestScores().size() > 3)?level.getBestScores().get(3):0);
		initialValues.put(LevelConstants.KEY_FIFTH_BEST_SCORE, 
				(level.getBestScores().size() > 4)?level.getBestScores().get(4):0);
		
		db.insert(LevelConstants.DATABASE_LEVELS_TABLE, null, initialValues);
	}
	
	public Level getLevel(Integer level) {
		Cursor cursor = db.query(LevelConstants.DATABASE_LEVELS_TABLE, 
				new String[]{LevelConstants.KEY_BALLS,LevelConstants.KEY_BALLS_RADIO,LevelConstants.KEY_SPEED,
					LevelConstants.KEY_UNLOCKED,LevelConstants.KEY_BEST_SCORE,LevelConstants.KEY_SECOND_BEST_SCORE,
					LevelConstants.KEY_THIRD_BEST_SCORE,LevelConstants.KEY_FOURTH_BEST_SCORE,LevelConstants.KEY_FIFTH_BEST_SCORE}
			, LevelConstants.KEY_LEVEL + "="+level, null, null, null, null);
		if(cursor == null || cursor.getCount() == 0) {
			return null;
		}
		cursor.moveToFirst();
		if(cursor.isAfterLast() || cursor.isBeforeFirst()) {
			return null;
		}
		
		
		List<Integer> bestScores = new ArrayList<Integer>();
		bestScores.add(cursor.getInt(cursor.getColumnIndex(LevelConstants.KEY_BEST_SCORE)));
		bestScores.add(cursor.getInt(cursor.getColumnIndex(LevelConstants.KEY_SECOND_BEST_SCORE)));
		bestScores.add(cursor.getInt(cursor.getColumnIndex(LevelConstants.KEY_THIRD_BEST_SCORE)));
		bestScores.add(cursor.getInt(cursor.getColumnIndex(LevelConstants.KEY_FOURTH_BEST_SCORE)));
		bestScores.add(cursor.getInt(cursor.getColumnIndex(LevelConstants.KEY_FIFTH_BEST_SCORE)));

		Integer balls = cursor.getInt(cursor.getColumnIndex(LevelConstants.KEY_BALLS));
		Integer speed = cursor.getInt(cursor.getColumnIndex(LevelConstants.KEY_SPEED));
		Integer ballsRadio = cursor.getInt(cursor.getColumnIndex(LevelConstants.KEY_BALLS_RADIO));
		String unlocked = cursor.getString(cursor.getColumnIndex(LevelConstants.KEY_UNLOCKED));
		
		Level levelObject = new Level(level, bestScores, balls, speed, ballsRadio, unlocked=="true");
		return levelObject;
	}
	
	public Integer getLastLevel() {
		Cursor cursor = db.query(LevelConstants.DATABASE_LEVELS_TABLE,
				new String[] { LevelConstants.KEY_LEVEL }, null, null, null,
				null, LevelConstants.KEY_LEVEL + " DESC");
		if(cursor == null || cursor.getCount() == 0) {
			return null;
		}
		cursor.moveToFirst();
		if(cursor.isAfterLast() || cursor.isBeforeFirst()) {
			return null;
		}
		return cursor.getInt(cursor.getColumnIndex(LevelConstants.KEY_LEVEL));
	}
	
	public Boolean existsSomeLevels() {
		Cursor cursor = db.query(LevelConstants.DATABASE_LEVELS_TABLE, 
				new String[]{LevelConstants.KEY_LEVEL}, "1 = 1", null, null, null, null);
		if(cursor == null || cursor.getCount() == 0) {
			return false;
		}
		return true;
	}
}
